package Server.Socket;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Server extends JFrame implements ActionListener {
  //服务器主程序负责界面及服务器主线程serverThread启动
  //服务端主线程serverThread又产生BroadCast及ClientThread线程
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//建立服务器端主界面中所需布局方式
	BorderLayout borderLayout1 = new BorderLayout();
	BorderLayout borderLayout2 = new BorderLayout();
	
	JPanel jPanel1 = new JPanel();
	JPanel jPanel2 = new JPanel();
	JButton jButton1 = new JButton();
	JButton jButton2 = new JButton();
	JScrollPane jScrollPane1 = new JScrollPane();
	
	//创建接受信息文本框
	static JTextArea jTextArea1 = new JTextArea();
	boolean bool = false,start = false;
	int i = 0;
	ServerThread serverThread;
	Thread thread;
	
	public Server(){
		getContentPane().setLayout(borderLayout1);
		
		jButton1.setText("关闭服务器");
		jButton1.addActionListener(this);
		jButton2.setText("开启服务器");
		jButton2.addActionListener(this);
		
		this.getContentPane().add(jPanel1,java.awt.BorderLayout.NORTH);
		jPanel1.add(jButton2);
		jPanel1.add(jButton1);
		
		this.getContentPane().add(jPanel2,java.awt.BorderLayout.CENTER);
		jTextArea1.setText("");
		jTextArea1.setEditable(false);
		jPanel2.setLayout(borderLayout2);
		jPanel2.add(jScrollPane1,java.awt.BorderLayout.CENTER);
		jScrollPane1.getViewport().add(jTextArea1);
		
		this.setSize(400,400);
		this.setVisible(true);
	}
	
	public static void main(String[] args){
		Server server = new Server();
		server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == jButton1)
		{
			bool = false;
			start = false;
			serverThread.finalize();
			this.setVisible(false);
		}
		else if(e.getSource() == jButton2)
		{
			serverThread = new ServerThread();
			serverThread.start();
		}
		
	}
	
	
}
